home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / main / translate.c < prev    next >
Text File  |  1994-03-21  |  8KB  |  305 lines

  1. /*
  2. *    translate.c
  3. *    written by Roland Månsson, Lund University Computing Center, Sweden
  4. *    roland_m@ldc.lu.se
  5. *    July 1992
  6. *
  7. *    Modified by Pascal Maes
  8. *    UCL/ELEC
  9. *    Place du Levant, 3
  10. *    B-1348 Louvain-la-Neuve
  11. *****************************************************************
  12. *    Part of:                                                    *
  13. *    NCSA Telnet for the Macintosh                                *
  14. *                                                                *
  15. *    National Center for Supercomputing Applications                *
  16. *    Software Development Group                                    *
  17. *    152 Computing Applications Building                            *
  18. *    605 E. Springfield Ave.                                        *
  19. *    Champaign, IL  61820                                        *
  20. *                                                                *
  21. *    Copyright (c) 1993,                                            *
  22. *    Board of Trustees of the University of Illinois                *
  23. *****************************************************************
  24. *    Modified 7/93 by Jim Browne for NCSA.
  25. */
  26.  
  27. #ifdef MPW
  28. #pragma segment 4
  29. #endif
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "TelnetHeader.h"
  34. #include "debug.h"
  35. #include "wind.h"
  36. #include "vsdata.h"
  37. #include "telneterrors.h"
  38. #include "translate.proto.h"
  39. #include "vsinterf.proto.h"
  40.  
  41. //#define    DEBUG_TRANSLATION
  42. /*************** external variables ***************/
  43.  
  44.  
  45. extern     WindRec *screens;        /* The screen array from maclook.c */
  46. extern    short scrn;                /* The current screen from maclook.c */
  47.  
  48. /*************** global variables ***************/
  49.  
  50. BytePtr    DefaultTable,
  51.         FTPinTable,
  52.         FTPoutTable;
  53.         
  54. Handle    transTablesHdl;
  55. short    nNational;
  56.  
  57. #if 0
  58. Boolean get_trsl (short id, Byte **table)
  59. {
  60.     Handle h;
  61.     long size;
  62.  
  63.     h = GetResource (TRSL,id);
  64.  
  65.     if ((h==NULL) || (ResError()!=noErr)) 
  66.         {
  67.         DoError(106 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  68.         return (FALSE);
  69.         }
  70.         
  71.     size = GetHandleSize(h);
  72.     if (size != 256) 
  73.         {
  74.         DoError(107 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  75.         return (FALSE);
  76.         }
  77.  
  78.     HLockHi(h);
  79.     *table = (Byte *) *h;
  80.     return (TRUE);
  81. }
  82. #endif
  83.  
  84. short    transBuffer(short oldtable, short newtable)        /* translate entire buffer */
  85. {
  86.     VSscrn    *vsscreen;
  87.     VSline    *vslin,*p;
  88.     short        lineNo,maxLineNo;
  89.     short        width;                /* allocated witdth of window (80/132) */
  90.     char        tmp[80];                /* only for debugging */
  91.     
  92.     vsscreen = VSwhereis (screens[scrn].vs);
  93.     vslin = vsscreen->buftop;
  94.  
  95. #ifdef DEBUG_TRANSLATION
  96.     putln ("in transBuffer, well and alive");
  97.     sprintf (tmp,"VSgetlines(screens[scrn].vs):%d", VSgetlines(screens[scrn].vs)); putln (tmp);
  98.     sprintf (tmp,"VSmaxwidth(screens[scrn].vs):%d", VSmaxwidth(screens[scrn].vs)); putln (tmp);
  99.     sprintf (tmp,"vsscreen->lines:%d\n",vsscreen->lines); putln (tmp);
  100.     sprintf (tmp,"vsscreen->maxlines:%d\n",vsscreen->maxlines); putln (tmp);
  101.     sprintf (tmp,"vsscreen->numlines:%d\n",vsscreen->numlines); putln (tmp);
  102.     sprintf (tmp,"vsscreen->allwidth:%d\n",vsscreen->allwidth); putln (tmp);
  103.     sprintf (tmp,"vsscreen->maxwidth:%d\n",vsscreen->maxwidth); putln (tmp);
  104.     sprintf (tmp,"vsscreen:%08x\n",vsscreen);  putln (tmp);
  105.     sprintf (tmp,"vslin:%08x\n",vslin);  putln (tmp);
  106.     sprintf (tmp,"next:%08x\n",vslin->next);  putln (tmp);
  107.     sprintf (tmp,"prev:%08x\n",vslin->prev);  putln (tmp);
  108.     sprintf (tmp,"text:%08x\n\n",vslin->text);  putln (tmp);
  109. #endif
  110.  
  111.     width = VSmaxwidth(screens[scrn].vs)+1;    /* VSmaxwidth returns 79 or 131 */
  112.     p = vslin;
  113.     maxLineNo = vsscreen->numlines+VSgetlines(screens[scrn].vs); /* VSgetlines returns 24 or whatever */
  114.     for (lineNo=1; lineNo<=maxLineNo; lineNo++) {
  115.         /*sprintf(tmp,"lineNo:%d, p:%08x, starts with:%c%c%c%c",lineNo,p,*(p->text),*(p->text+1),*(p->text+2),*(p->text+3)); putln(tmp);*/
  116.         if (p==NULL) { putln ("p is NULL"); return (-1); }
  117.         if (p->text==NULL) { putln ("p->text is NULL"); return (-1); }
  118.         
  119.         // First convert the line back to Mac US format, and then to the new format.
  120.         trbuf_nat_mac((unsigned char *)p->text,width, oldtable);
  121.         trbuf_mac_nat((unsigned char *)p->text,width, newtable);
  122.         
  123.         p = p->next;
  124.     }
  125.     sprintf (tmp, "transBuffer:did convert %d lines", lineNo-1); putln (tmp);
  126. }
  127.  
  128. BytePtr        GetTranslationResource(short id)
  129. {
  130.     Handle    h;
  131.     
  132.     h = GetResource(MY_TRSL, id);
  133.     
  134.     if ((h == NULL) || (ResError() != noErr)) {
  135.         // Do nasty mean error here. BUGG
  136.         }
  137.     
  138.     DetachResource(h);
  139.     HLockHi(h);
  140.     return((BytePtr) *h);
  141. }
  142.  
  143. //    table #'s 1...n correspond to tables in our master array, table #0 is the default table
  144. BytePtr        ReturnTablePtr(short table, Boolean out)
  145. {
  146.     if (table > nNational || table < 1) return(DefaultTable + ((out == TRUE) * 256));
  147.     return((BytePtr)(*transTablesHdl + ((table - 1) * 512) + ((out == TRUE) * 256)));
  148. }
  149.  
  150. //    The Default table (i.e. no translation) and the two FTP tables are stored in the 
  151. //     Application's resource fork as resources of type TRSL.  The
  152. //     tables added and removed by the user are stored in the prefs file as resources of
  153. //     type taBL.  This routine loads the default table and the two FTP tables into memory.
  154. //     Failures can only be caused by an incorrect application resource fork.
  155. void    Setup_Default_Tables(void)
  156. {
  157.     DefaultTable = GetTranslationResource(TRSL_DEFAULT_TABLE);
  158.     FTPinTable = GetTranslationResource(TRSL_FTP_TABLE);
  159.     FTPoutTable = FTPinTable + 256;
  160. }
  161.  
  162. /* 
  163. *    Be very careful with calling putln from this procedure, since
  164. *    putln uses the translation tables. If the tables are not setup
  165. *    garbage output will appear. This is not harmful, but very
  166. *    annoying.
  167. */
  168.  
  169. void trInit (MenuHandle    whichMenu)
  170. {
  171.     short    i, nRestaBL;        /* mp: # of resources of type taBL */
  172.     Handle    h;
  173.     short    rsrcID;
  174.     ResType    rsrcType;
  175.     Str255    rsrcName;
  176.     
  177.         
  178.      nNational = 0;
  179.     Setup_Default_Tables();
  180.     transTablesHdl = NewHandle(0);
  181.     
  182.     if (nRestaBL = CountResources(USER_TRSL)) {
  183.         for (i = 1; i <= nRestaBL; i++)
  184.         {
  185.             h = GetIndResource(USER_TRSL, i);
  186.             if (ResError() == noErr && (GetHandleSize(h) == 512)) {
  187.                 GetResInfo(h, &rsrcID, &rsrcType, rsrcName);
  188.                 nNational++;
  189.  
  190.                 // Insert the table's name in the Translation menu
  191.                 AppendMenu(whichMenu, "\pDoh");
  192.                 SetItem(whichMenu, nNational+1, rsrcName);    // No metas!
  193.                 
  194.                 // Now append the table's data to the master array of table data
  195.                 HUnlock(transTablesHdl);
  196.                 SetHandleSize(transTablesHdl, (nNational * 512));
  197.                 HLockHi(transTablesHdl);
  198.                 HLock(h);
  199.                 BlockMove(*h, (*transTablesHdl) + ((nNational - 1) * 512), 512);
  200.                 
  201.                 // Release the resource
  202.                 ReleaseResource(h);
  203.                 }
  204.         }
  205.  
  206.     }
  207.     
  208.     CalcMenuSize(whichMenu);
  209. }
  210.  
  211.  
  212. /*    Converts a char from 8-bit National to 8-bit Macintosh */
  213. unsigned char    nat_mac(unsigned char *ascii, short table)
  214. {
  215.     short    b;
  216.     BytePtr    table_data = ReturnTablePtr(table, FALSE);
  217.     
  218.     b = (short) *ascii;
  219.     *ascii = table_data[b];
  220.     return (*ascii);
  221. }
  222.  
  223. unsigned char    ftp_iso_mac(unsigned char *ascii)
  224. {
  225.     short    b;
  226.     
  227.     b = (short) *ascii;
  228.     *ascii = FTPinTable[b];
  229.     return (*ascii);
  230. }
  231.  
  232. void    trbuf_nat_mac(unsigned char *buf, short len, short table)
  233. {
  234.     short            i;
  235.     unsigned char    ascii;
  236.     unsigned char    *p;
  237.  
  238.     for (i=0,p=buf; i<len; i++,p++)
  239.     {
  240.         ascii = *p;
  241.         *p = nat_mac(&ascii, table);
  242.     }
  243.             
  244. }
  245.  
  246. void    trbuf_ftp_mac(unsigned char *buf, short len)
  247. {
  248.     short            i;
  249.     unsigned char    ascii;
  250.     unsigned char    *p;
  251.  
  252.     for (i=0,p=buf; i<len; i++,p++)
  253.     {
  254.         ascii = *p;
  255.         *p = ftp_iso_mac(&ascii);
  256.     }
  257.             
  258. }
  259.  
  260.  
  261. /*    Converts a char from 8-bit Macintosh to 8-bit National */
  262. unsigned char    mac_nat(unsigned char *ascii, short table)
  263. {
  264.     short    b;
  265.     BytePtr    table_data = ReturnTablePtr(table, TRUE);
  266.     
  267.     b = (short) *ascii;
  268.     *ascii = table_data[b];
  269.     return (*ascii);
  270. }
  271.  
  272. unsigned char    ftp_mac_iso(unsigned char *ascii)
  273. {
  274.     short    b;
  275.     
  276.     b = (short) *ascii;
  277.     *ascii = FTPoutTable[b];
  278.     return (*ascii);
  279. }
  280.  
  281. void    trbuf_mac_nat(unsigned char *buf, short len, short table)
  282. {
  283.     short            i;
  284.     unsigned char    ascii;
  285.     unsigned char    *p;
  286.     
  287.     for (i=0,p=buf; i<len; i++,p++)
  288.     {
  289.         ascii = *p;
  290.         *p = mac_nat(&ascii, table);
  291.     }
  292. }
  293.  
  294. void    trbuf_mac_ftp(unsigned char *buf, short len)
  295. {
  296.     short            i;
  297.     unsigned char    ascii;
  298.     unsigned char    *p;
  299.     
  300.     for (i=0,p=buf; i<len; i++,p++)
  301.     {
  302.         ascii = *p;
  303.         *p = ftp_mac_iso(&ascii);
  304.     }
  305. }